package com.eugene.process;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eugene.dao.MarketVariableDAO;
import com.eugene.dao.VcvDAO;
import com.eugene.element.FnCalendar;
import com.eugene.entity.MvData;
import com.eugene.entity.VaRScenario;

public class WriteScenarioRst {
	private final static Logger logger = 
		LoggerFactory.getLogger(WriteScenarioRst.class);
	/**
	 * @param args
	 */
	public static void writeScenario(FnCalendar baseDate){
		
	}
	public static void writeScenario(String sceSetId, FnCalendar baseDate){
		
	}
	
	public static void writeRmScenarioRf(FnCalendar baseDate){
		List<VaRScenario> rst = new ArrayList<VaRScenario>();
		List<VaRScenario> stressRf = VcvDAO.getScenario(baseDate);
		int count =0;
		for( VaRScenario aa: stressRf){
			count =0 ;
			List<MvData> rfHis = WriteScenarioRst.getStressRfHis(baseDate, aa.getScenarioId());
			for( MvData zz : rfHis){
				count = count+1;
				for(MvData xx : rfHis){
					VaRScenario temp = new VaRScenario(baseDate,aa.getScenarioId(),"RM"
												,count, xx.getMvDataId(),xx.getValue(),xx.getValue());
					if(zz.equals(xx)){
						temp.setRfSceValue(xx.getValue()* 1.001);
					}
					temp.setTweakedRfId(zz.getMvDataId());
					temp.setLastUpdate(Calendar.getInstance().getTime());
					rst.add(temp);
				}
			}
		}
//		normal VaR
		List<MvData> rfHis =VcvDAO.getRfHis(baseDate);
		count =0;
		for( MvData zz : rfHis){
			count = count+1;
			for(MvData xx : rfHis){
				VaRScenario temp = new VaRScenario(baseDate,"Base","RM"
											,count, xx.getMvDataId(),xx.getValue(),xx.getValue());
				if(zz.equals(xx)){
					temp.setRfSceValue(xx.getValue()* 1.001);
				}
				temp.setTweakedRfId(zz.getMvDataId());
				temp.setLastUpdate(Calendar.getInstance().getTime());
				rst.add(temp);
			}
		}
		VcvDAO.setScenarioHis(rst);

	}
	
	public static List<MvData> getStressRfHis(FnCalendar baseDate, String sceId){
		List<MvData> rfHis = new ArrayList<MvData>();
		List<VaRScenario> sceDetails = new ArrayList<VaRScenario>();
		List<MvData> rst = new ArrayList<MvData>();

		rfHis = VcvDAO.getRfHis(baseDate);
		sceDetails = VcvDAO.getScenarioDetail(baseDate,sceId);
		
		for( MvData aa : rfHis){
			MvData temp = new MvData(baseDate, aa.getMvDataId(),aa.getValue());
			for (VaRScenario bb : sceDetails){
				if(aa.getMvDataId().equals(bb.getRfId())){
					switch (bb.getSceType()) {
					case ADD:
						temp.setValue(aa.getValue() + bb.getTweakValue());
						break;
					case MULTI:
						temp.setValue(aa.getValue()* bb.getTweakValue());
						break;
					case KEY_IN:
						temp.setValue(bb.getTweakValue());
						break;
					default:
						break;
					}
				}
			}		
			rst.add(temp);
		}
		return rst;
	}
	
	public static void writeHsScenarioRf(FnCalendar baseDate){
		List<VaRScenario> rst = new ArrayList<VaRScenario>();
		List<VaRScenario> stressRf = VcvDAO.getScenario(baseDate);
		List<MvData> rf = MarketVariableDAO.getRiskFactor();
		Map<MvData, List<MvData>> mvHistMap = new LinkedHashMap<MvData, List<MvData>>();
		Map<MvData, List<MvData>> mvRatesMap = new LinkedHashMap<MvData, List<MvData>>();
		int count =0;
		int timeHorizon =1;
		
//		history data fetch & map
		for(MvData bb: rf){
			List<MvData> data1= new ArrayList<MvData>();
			logger.debug("mvDataId :{}", bb.getMvDataId());
			data1 = MarketVariableDAO.getRfHistoryData(bb.getMvDataId(), baseDate, 250);
			mvHistMap.put(bb, data1);
//			data1.clear();
		}
		
		for(MvData aa: mvHistMap.keySet()){
			List<MvData> data1 = new ArrayList<MvData>();
			List<MvData> rates = new ArrayList<MvData>();
			
			data1 = mvHistMap.get(aa);
			logger.debug("size : {}", data1.size());
			
			for(int i =0 ; i< data1.size()- timeHorizon; i++){
				try {
					rates.add((MvData)data1.get(i).clone());
				} catch (CloneNotSupportedException e) {
					e.printStackTrace();
				}
				logger.debug("data1,2 :{}, {}" , data1.get(i).getValue(), data1.get(i+timeHorizon).getValue());
				rates.get(i).setValue(Math.log(data1.get(i).getValue() / data1.get(i+timeHorizon).getValue()));
				logger.debug("rates :{},{}" ,rates.get(i).getValue(),data1.get(i).getMvDataId());
			}
			
			mvRatesMap.put(aa, rates);
		}
		
		for( VaRScenario aa: stressRf){
			List<MvData> rfHis = WriteScenarioRst.getStressRfHis(baseDate, aa.getScenarioId());
			for( MvData zz :rfHis){
				count =0;
				for(MvData xx : mvRatesMap.get(zz)){
					count=count+1;
					VaRScenario temp = new VaRScenario(baseDate,aa.getScenarioId(),"HS"
							,count, zz.getMvDataId(),zz.getValue(),zz.getValue());
					temp.setRfSceValue(zz.getValue() * Math.exp(xx.getValue()));
					temp.setTweakedRfId(xx.getBaseDate().toString());
					rst.add(temp);
				}
			}
		}
		
		VcvDAO.setScenarioHis(rst);
	}
	
}
